package com.yanchangapp.ui.presenter;

import com.zxl.baselib.bean.response.BaseResponse;
import com.zxl.baselib.ui.base.BaseActivity;
import com.zxl.baselib.ui.base.BaseObserver;
import com.zxl.baselib.ui.base.BasePresenter;
import com.zxl.baselib.ui.base.ExceptionHandle;
import com.yanchangapp.base.HeadApiRetrofit;
import com.yanchangapp.commom.AppConstant;
import com.yanchangapp.model.request.SaveDraftRequest;
import com.yanchangapp.model.request.SendTotalMailRequest;
import com.yanchangapp.model.response.MailDetailResponse;
import com.yanchangapp.ui.view.TnspondAtView;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by apple on 2019/7/12.
 * description:转发回复控制p层
 */
public class TnspondAtPresenter extends BasePresenter<TnspondAtView> {

    public TnspondAtPresenter(BaseActivity context) {
        super(context);
    }


    public void queryMailDetailInfo(int maidID){
        HeadApiRetrofit.getInstance().queryMailDetailInfo(maidID)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<MailDetailResponse>() {
                    @Override
                    public void onNext(MailDetailResponse mailDetailResponse) {
                        if (mailDetailResponse.getCode() == AppConstant.REQUEST_SUCCESS){
                            if (getView() != null){
                                getView().queryMailDetailSuccess(mailDetailResponse.getData());
                            }
                        }else {
                            if (getView() != null){
                                getView().queryMailDetailFailure(mailDetailResponse.getMsg());
                            }
                        }
                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseException exception) {
                        if (getView() != null){
                            getView().queryMailDetailFailure("获取邮件详情失败");
                        }
                    }
                });
    }

    public void sendMail(SendTotalMailRequest sendTotalMailRequest){
        if (getView() != null){
            getView().showLoadingDialog();
        }
        HeadApiRetrofit.getInstance().sendTotalMail(sendTotalMailRequest)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<BaseResponse>() {
                    @Override
                    public void onNext(BaseResponse baseResponse) {
                        if (getView() != null){
                            getView().hideLoadingDialog();
                        }
                        if (baseResponse.getCode() == AppConstant.REQUEST_SUCCESS){
                            if (getView() != null){
                                getView().sendMailSuccess();
                            }
                        }else {
                            if (getView() != null){
                                getView().sendMailFailure(baseResponse.getMsg());
                            }
                        }
                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseException exception) {
                        if (getView() != null){
                            getView().hideLoadingDialog();
                            getView().sendMailFailure("发送失败");
                        }
                    }
                });
    }


    // 保存草稿
    public void saveDraftMail(SaveDraftRequest saveDraftRequest){
        if (getView() != null){
            getView().showLoadingDialog();
        }
        HeadApiRetrofit.getInstance().saveMailToDraft(saveDraftRequest)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<BaseResponse>() {
                    @Override
                    public void onNext(BaseResponse baseResponse) {
                        if (getView() != null){
                            getView().hideLoadingDialog();
                        }
                        if (baseResponse.getCode() == AppConstant.REQUEST_SUCCESS){
                            if (getView() != null){
                                getView().saveDraftSuccess();
                            }
                        }else {
                            if (getView() != null){
                                getView().hideLoadingDialog();
                                getView().saveDraftFailure(baseResponse.getMsg());
                            }
                        }
                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseException exception) {
                        if (getView() != null){
                            getView().hideLoadingDialog();
                            getView().saveDraftFailure("保存草稿失败");
                        }
                    }
                });
    }

}
